﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Adams.Public.Api
{
    public static class Security
    {
        public static string GenerateToken(int memberId)
        {
            var token = new DataTransfer.Security.Token();
            token.MemberID = memberId;
            token.CreatedOn = DateTime.UtcNow;

            string s = Shared.Serialize.SerializeXml(token);
            string en = Shared.Crypt.Encrypt(s);
            
            return en;
        }

        public static int GetMemberIDFromToken(string token)
        {
            if (string.IsNullOrEmpty(token))
            {
                return 0;
            }

            var t = GetToken(token);
            return t.MemberID;
        }

        public static DataTransfer.Security.Token GetToken(string token)
        {
            var s = Shared.Crypt.Decrypt(token);
            var t = Shared.Serialize.DeserializeXml<DataTransfer.Security.Token>(s);
            return t;
        }
    }
}